package rxretrofit_base.net;

import android.content.Context;
import android.widget.Toast;

import com.rxretrofit_build.net.RxSubscriber;
import com.rxretrofit_build.util.CustomLog;
import com.rxretrofit_build.util.CustomSharedPreferences;
import com.rxretrofit_build.util.MapUtil;

import master_bean.LoginBean;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.TokenBean;


/**
 * Title:
 * Description:
 * Copyright:Copyright(c)2016
 * Company: Cree
 * CreateTime:16/11/29  17:22
 *
 * @author cree
 * @version 1.0
 */
public abstract class BaseSubscriber<T> extends RxSubscriber<T> {

    public BaseSubscriber(Context context) {
        super(context);
    }
    private String[] message = {"提交", "保存","修改","更新"};
    private int[] code = {1, 2, 3, 7, 8, 10, 11};

    @Override
    public void onNext(T o) {
        super.onNext(o);
        if (o instanceof TokenBean) {
            TokenBean tokenBean = (TokenBean) o;

            if (((TokenBean) o).getStatus_code() == 0) {
                if(isContainsMessage(((TokenBean) o).getMessage())){
                    Toast.makeText(mContext, ((TokenBean) o).getMessage(), Toast.LENGTH_SHORT).show();
                }
            } else {
                CustomLog.d("进来啊!= 1");
                if (isContainsCode(((TokenBean) o).getStatus_code())) {

                    Toast.makeText(mContext, ((TokenBean) o).getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
            //---------------------------请求回来都打印返回的信息---------------------------------
            if (tokenBean.getData() instanceof LoginBean) {
                LoginBean dataBean = (LoginBean) tokenBean.getData();
                saveData(KeyBean.APP_KEY, dataBean.getApp_key());
            }
        }
        onSuccess(o);
    }

    private boolean isContainsCode(int status_code) {
        for (int i = 0; i < this.code.length; i++) {
            if(status_code ==this.code[i]){
                return true;
            }
        }
        return false;
    }

    private boolean isContainsMessage(String message) {
        for (int i = 0; i < this.message.length; i++) {
            if(message.contains(this.message[i])){
                return true;
            }
        }
        return false;
    }


    /**
     * 保存数据的简化代码
     *
     * @param bean  枚举 key值
     * @param value
     */
    private void saveData(KeyBean bean, String value) {
        CustomSharedPreferences.saveString(mContext
                , new MapUtil()
                        .put(bean.valueOf(), value)
                        .build()
                , KeyBean.FILE_NAME.valueOf());
    }
    @Override
    public void onError(Throwable e) {
        super.onError(e);
        e.printStackTrace();
        CustomLog.d(e);
        onFailure(e);
//        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
//        MobclickAgent.reportError(context,e);   //友盟Error统计
    }

    public abstract void onSuccess(T o);

    public abstract void onFailure(Throwable e);
}
